package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ClueController
 * @Author : 司猫子
 * @Date: 2021/1/7 21:33
 */
@Controller
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private DicTypeService dicTypeService;
    @Autowired
    private DicValueService dicValueService;

    public int saveCreatClue(Clue clue) {
        return clueService.saveCreatClue(clue);
    }

    public Clue queryClueDetailById(String id) {
        return clueService.queryClueDetailById(id);
    }

    @RequestMapping("/workbench/clue/index.do")
    public String toClueIndex(Model model){
        List<User> userList = userService.selectAllUsers();
        List<DicValue> appellationList = dicValueService.selectDicValueByTypeCode("appellation");
        List<DicValue> clueStateList = dicValueService.selectDicValueByTypeCode("clueState");
        List<DicValue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        model.addAttribute("userList",userList);
        model.addAttribute("appellationList",appellationList);
        model.addAttribute("clueStateList",clueStateList);
        model.addAttribute("sourceList",sourceList);

        return "workbench/clue/index";
    }

    @RequestMapping("/workbench/clue/queryClueForPageByCondition.do")
    @ResponseBody
    public Object queryClueForPageByCondition(int pageNo, int pageSize,String fullName,String company,String phone,String mphone,String source,String owner,String state) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beginNo",(pageNo - 1) * pageSize);
        map.put("pageSize",pageSize);
        map.put("fullName",fullName);
        map.put("company",company);
        map.put("phone",phone);
        map.put("mphone",mphone);
        map.put("source",source);
        map.put("owner",owner);
        map.put("state",state);
        List<Clue> clueList = clueService.selectClueForPageByCondition(map);
        long l = clueService.selectCountOfClueByCondition(map);
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("clueList", clueList);
        retMap.put("totalRows", l);

        return retMap;
    }

    @RequestMapping("/workbench/clue/deleteClueByIds.do")
    @ResponseBody
    public Object deleteClueByIds(String[] id) {
        ReturnObject returnObject = new ReturnObject();
        int i = clueService.deleteClueByIds(id);
        if (i == 0) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败,请重新操作");
        } else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/editClue.do")
    @ResponseBody
    public Object editClue(String id){
        Clue clue = clueService.selectClueById(id);
        return clue;
    }

    @RequestMapping("/workbench/clue/saveEditClue.do")
    @ResponseBody
    public Object saveEditClue(HttpSession session, Clue clue) {
        ReturnObject returnObject = new ReturnObject();
        User user = (User) session.getAttribute(Contants.SUSSION_USER);
        clue.setEditTime(DateUtils.formatDateTime(new Date()));
        clue.setEditBy(user.getId());


        int i = clueService.updateByPrimaryKey(clue);
        if (i == 1) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);

        } else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加线索失败");
        }

        return returnObject;
    }

    @RequestMapping("/workbench/clue/saveCreateClue.do")
    @ResponseBody
    public Object saveClue(HttpSession session, Clue clue) {
        ReturnObject returnObject = new ReturnObject();
        User user = (User) session.getAttribute(Contants.SUSSION_USER);
        clue.setId(UUIDUtils.getUUID());
        clue.setCreateTime(DateUtils.formatDateTime(new Date()));
        clue.setCreateBy(user.getId());


        int i = clueService.insertClue(clue);
        if (i == 1) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);

        } else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加线索失败");
        }

        return returnObject;
    }

    @RequestMapping("/workbench/clue/convertClue.do")
    public Object convertClue(String id,Model model){
        Clue clue = clueService.queryClueDetailById(id);
        List<DicValue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        model.addAttribute("clue", clue);
        model.addAttribute("stageList", stageList);
        return "workbench/clue/convert";
    }

    @RequestMapping("/workbench/clue/saveConvertClue.do")
    @ResponseBody
    public Object saveConvertClue(HttpSession session,String clueId,String isCreateTran,String amountOfMoney,String tradeName,String expectedClosingDate,String stage,String activityId) {
        //封装参数
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("clueId",clueId);
        map.put("isCreateTran",isCreateTran);
        map.put("amountOfMoney",amountOfMoney);
        map.put("tradeName",tradeName);
        map.put("expectedClosingDate",expectedClosingDate);
        map.put("stage",stage);
        map.put("activityId",activityId);
        map.put("sessionUser",session.getAttribute(Contants.SUSSION_USER));

        ReturnObject returnObject=new ReturnObject();
        try {
            //调用service层方法，保存线索转换
            clueService.saveConvert(map);

            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试....");
        }

        return returnObject;
    }
}
